<template>
	<view>
		<view>
			<u-search shape="square" actionText="添加" @custom="addClient"></u-search>
		</view>
		<view class="u-page">
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item, index) in indexList" :key="index">
					<u-cell :title="`${item.name}-${index + 1}`" @click="handleClick(item.name)">
						<u-avatar slot="icon" shape="square" size="35" :src="item.url"
							customStyle="margin: -3px 5px -3px 0">
						</u-avatar>
					</u-cell>
				</u-list-item>
			</u-list>

		</view>

	</view>
</template>

<script>
	import {
		getIngredientList
	} from '@/api/ingredients.js'
	export default {
		data() {
			return {
				indexList: []
			};
		},
		onLoad() {
			// this.indexList = [];
			// this.loadmore();
		},
		onShow() {
			this.indexList = [];
			getIngredientList()
				.then(res => {
					res.data.forEach(item => {
						this.indexList.push(item);
					});

				})
				.catch(error => {
					uni.$u.toast(error.errMsg)
					console.log(error)
				})
		},
		methods: {
			scrolltolower() {
				this.loadmore();
			},
			loadmore() {

				getIngredientList()
					.then(res => {
						res.data.forEach(item => {
							this.indexList.push(item);
						});

					})
					.catch(error => {
						uni.$u.toast(error)
						console.log(error)
					})

			},

			addClient() {
				uni.navigateTo({
					url: '/pages/ingredients/components/form'
				});
			},
			handleClick(name) {
				console.log(name)
			}
		},
	}
</script>

<style lang="scss">
	.u-page {
		padding: 0;
	}
</style>